package com.party.mobile.web.controller.member;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.Member;
import com.party.core.model.message.IdentityType;
import com.party.core.model.message.Message;
import com.party.core.service.message.IMessageService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: Administrator.
 * @Description: 合伙人消息控制器
 * @Date:Created in 2017/10/31 0031 上午 9:26.
 */
@Controller
@RequestMapping("copartner/message")
public class CopartnerMessageController {
    @Autowired
    private IMessageService messageService;

    @Autowired
    private CurrentUserBizService currentUserBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 消息列表
     *
     * @param message 消息
     * @param page    分页参数
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "list")
    public AjaxResult list(Message message, Page page, HttpServletRequest request) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            message.setCreateBy(memberId);
            message.setIdentityType(IdentityType.PARTNER.getCode());
            List<Message> list = messageService.listPage(message, page);
            return AjaxResult.success(list, page);
        } catch (Exception e) {
            logger.error("消息列表异常", e);
            return AjaxResult.error(100, "异常");
        }
    }

    /**
     * 消息列表
     *
     * @param message 消息
     * @param page    分页参数
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "channelDisList")
    public AjaxResult channelDisList(Message message, Page page, HttpServletRequest request) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            message.setCreateBy(memberId);
            message.setIdentityType(IdentityType.CHANNEL_DIS.getCode());
            List<Message> list = messageService.listPage(message, page);
            return AjaxResult.success(list, page);
        } catch (Exception e) {
            logger.error("channelDisList 消息列表异常", e);
            return AjaxResult.error(100, "异常");
        }
    }

    /**
     * 消息列表
     *
     * @param message 消息
     * @param page    分页参数
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "cosponsorList")
    public AjaxResult cosponsorList(Message message, Page page, HttpServletRequest request) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            message.setCreateBy(memberId);
            message.setIdentityType(IdentityType.COSPONSOR.getCode());
            List<Message> list = messageService.listPage(message, page);
            return AjaxResult.success(list, page);
        } catch (Exception e) {
            logger.error("cosponsorList 消息列表异常", e);
            return AjaxResult.error(100, "异常");
        }
    }

    /**
     * 消息详情
     *
     * @param msgId
     * @return
     */
    @ResponseBody
    @RequestMapping("getDetails")
    public AjaxResult getDetails(String msgId) {
        if (StringUtils.isEmpty(msgId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "id不能为空");
        }

        Message message = messageService.get(msgId);
        return AjaxResult.success(message);
    }
}
